/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.sys.modular.pushsearch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.sys.modular.pushmanage.entity.PushManage;
import vip.xiaonuo.sys.modular.pushmanage.service.PushManageService;
import vip.xiaonuo.sys.modular.pushsearch.entity.PushSearch;
import vip.xiaonuo.sys.modular.pushsearch.enums.PushSearchExceptionEnum;
import vip.xiaonuo.sys.modular.pushsearch.mapper.PushSearchMapper;
import vip.xiaonuo.sys.modular.pushsearch.param.PushSearchParam;
import vip.xiaonuo.sys.modular.pushsearch.service.PushSearchService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 线上热门搜索service接口实现类
 *
 * @author fenmou
 * @date 2024-10-28 20:57:32
 */
@Service
public class PushSearchServiceImpl extends ServiceImpl<PushSearchMapper, PushSearch> implements PushSearchService {

    @Resource
    private PushManageService pushManageService;

    @Override
    public PageResult<PushSearch> page(PushSearchParam pushSearchParam) {
        QueryWrapper<PushSearch> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(pushSearchParam)) {

            // 根据订单编号 查询
            if (ObjectUtil.isNotEmpty(pushSearchParam.getOrderNumber())) {
                queryWrapper.lambda().eq(PushSearch::getOrderNumber, pushSearchParam.getOrderNumber());
            }
            // 根据订单状态 查询
            if (ObjectUtil.isNotEmpty(pushSearchParam.getOrderStatus())) {
                queryWrapper.lambda().eq(PushSearch::getOrderStatus, pushSearchParam.getOrderStatus());
            }
            // 根据投放时间 查询
            if (ObjectUtil.isNotEmpty(pushSearchParam.getPutTime())) {
                queryWrapper.lambda().eq(PushSearch::getPutTime, pushSearchParam.getPutTime());
            }
            // 根据终端数量 查询
            if (ObjectUtil.isNotEmpty(pushSearchParam.getTerminalNum())) {
                queryWrapper.lambda().eq(PushSearch::getTerminalNum, pushSearchParam.getTerminalNum());
            }
            // 根据城市 查询
            if (ObjectUtil.isNotEmpty(pushSearchParam.getCity())) {
                queryWrapper.lambda().eq(PushSearch::getCity, pushSearchParam.getCity());
            }
            // 根据播放时间 查询
            if (ObjectUtil.isNotEmpty(pushSearchParam.getPlayStartTime())) {
                queryWrapper.lambda().eq(PushSearch::getPlayStartTime, pushSearchParam.getPlayStartTime());
            }
            if (ObjectUtil.isNotEmpty(pushSearchParam.getPlayEndTime())) {
                queryWrapper.lambda().eq(PushSearch::getPlayEndTime, pushSearchParam.getPlayEndTime());
            }
            if (ObjectUtil.isNotEmpty(pushSearchParam.getCreateTime())) {
                queryWrapper.lambda().eq(PushSearch::getCreateTime, pushSearchParam.getCreateTime());
            }
//            // 根据状态（字典 0正常 1停用 2删除） 查询
//            if (ObjectUtil.isNotEmpty(pushSearchParam.getStatus())) {
//                queryWrapper.lambda().eq(PushSearch::getStatus, pushSearchParam.getStatus());
//            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<PushSearch> list(PushSearchParam pushSearchParam) {
        return this.list();
    }

    @Override
    public void add(PushSearchParam pushSearchParam) {
        PushManage pushManage = pushManageService.getById(pushSearchParam.getManagerId());
        if (ObjectUtil.isNotEmpty(pushManage)) {
            this.remove(new LambdaQueryWrapper<PushSearch>().eq(PushSearch::getManagerId, pushManage.getId()));
        }
        pushManage.setOrderNumber(pushSearchParam.getOrderNumber());
        pushManageService.updateById(pushManage);
//        PushSearch pushSearch = new PushSearch();
//        BeanUtil.copyProperties(pushSearchParam, pushSearch);
        this.save(BeanUtil.toBean(pushSearchParam,PushSearch.class));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<PushSearchParam> pushSearchParamList) {
        pushSearchParamList.forEach(pushSearchParam -> {
            this.removeById(pushSearchParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(PushSearchParam pushSearchParam) {
        PushSearch pushSearch = this.queryPushSearch(pushSearchParam);
        BeanUtil.copyProperties(pushSearchParam, pushSearch);
        this.updateById(pushSearch);
    }

    @Override
    public PushSearch detail(PushSearchParam pushSearchParam) {
        return this.queryPushSearch(pushSearchParam);
    }

    /**
     * 获取线上热门搜索
     *
     * @author fenmou
     * @date 2024-10-28 20:57:32
     */
    private PushSearch queryPushSearch(PushSearchParam pushSearchParam) {
        PushSearch pushSearch = this.getById(pushSearchParam.getId());
        if (ObjectUtil.isNull(pushSearch)) {
            throw new ServiceException(PushSearchExceptionEnum.NOT_EXIST);
        }
        return pushSearch;
    }

    @Override
    public void export(PushSearchParam pushSearchParam) {
        List<PushSearch> list = this.list(pushSearchParam);
        PoiUtil.exportExcelWithStream("SnowyPushSearch.xls", PushSearch.class, list);
    }

    @Override
    public PushSearch getDetailByMangerId(PushSearchParam pushSearchParam) {
        LambdaQueryWrapper<PushSearch> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNull(pushSearchParam.getManagerId())) {
            throw new ServiceException(PushSearchExceptionEnum.NOT_EXIST);
        }
        wrapper.eq(PushSearch::getManagerId, pushSearchParam.getManagerId());
        if (ObjectUtil.isNotNull(pushSearchParam)) {
            if (ObjectUtil.isNotEmpty(pushSearchParam.getStartTime()) && ObjectUtil.isNotEmpty(pushSearchParam.getEndTime())) {
                wrapper.between(PushSearch::getCreateTime, pushSearchParam.getStartTime(), pushSearchParam.getEndTime());
            }
        }
        PushSearch search = this.getOne(wrapper);
        if (ObjectUtil.isNull(search)) {
            return new PushSearch();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StrUtil.isNotEmpty(search.getPlayStartTime())){
            try {
                Date parse = simpleDateFormat.parse(search.getPlayStartTime());
                search.setPlayStartTime(simpleDateFormat.format(parse));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        if (StrUtil.isNotEmpty(search.getPlayEndTime())){
            try {
                Date parse = simpleDateFormat.parse(search.getPlayEndTime());
                search.setPlayEndTime(simpleDateFormat.format(parse));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        if (StrUtil.isNotEmpty(search.getPutTime())){
            try {
                Date parse = simpleDateFormat.parse(search.getPutTime());
                search.setPutTime(simpleDateFormat.format(parse));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return search;
    }

}
